package com.ultrapower.ctool.xn_app;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.bean.JingYingBean;
import com.ultrapower.ctool.bean.WangGeBean;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.http.BaseActivity;
import com.ultrapower.ctool.model.RegisterRolePirVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddWangGeActivity extends BaseActivity implements View.OnClickListener {
    //private TextView stype_addwg;//事件类型
    private Spinner stype_addwg_sp;//事件类型下拉框
    // private TextView step_addwg;//事件步骤
    private Spinner step_addwg_sp;//事件步骤下拉框
    private EditText dwid_addwg;//单位(数字)
    private EditText sname_addwg;//事件名
    private EditText sAddress_addwg;//事件发生地址
    private EditText trTableid_addwg;//关联的巡查表编号
    private TextView save_wg;
    private TextView cancle_wg;
    //private static final String[] stypes={"一般隐患","重大隐患","其他安全生产","违法生产经营活动"};
    private static final List<RegisterRolePirVO> stypes = new ArrayList<RegisterRolePirVO>();
    private static final List<RegisterRolePirVO> steps = new ArrayList<RegisterRolePirVO>();
    // private OkHttpClient client = new OkHttpClient();
    private Gson gson = new Gson();
    private CustomApplication application;
    //private static final String[] steps={"保存事件","处理事件","验收事件"};
    private ArrayAdapter<RegisterRolePirVO> stypeAdapter;
    private ArrayAdapter<RegisterRolePirVO> stepAdapter;
    private AdapterView.OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(18);
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "网格事件添加成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddWangGeActivity.this, WangGeActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "系统出现异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    static {
        steps.add(new RegisterRolePirVO("", ""));
        steps.add(new RegisterRolePirVO("1", "保存事件"));
        steps.add(new RegisterRolePirVO("2", "处理事件"));
        steps.add(new RegisterRolePirVO("3", "验收事件"));
        stypes.add(new RegisterRolePirVO("", ""));
        stypes.add(new RegisterRolePirVO("1", "一般隐患"));
        stypes.add(new RegisterRolePirVO("2", "重大隐患"));
        stypes.add(new RegisterRolePirVO("3", "违法生产经营行为"));
        stypes.add(new RegisterRolePirVO("4", "其他安全生产"));

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_wang_ge);
        application = (CustomApplication) getApplication();
        init();
    }

    private void init() {
        // stype_addwg = (TextView) findViewById(R.id.stype_addwg);
        stype_addwg_sp = (Spinner) findViewById(R.id.stype_addwg_sp);
        // step_addwg = (TextView) findViewById(R.id.step_addwg);
        step_addwg_sp = (Spinner) findViewById(R.id.step_addwg_sp);
        dwid_addwg = (EditText) findViewById(R.id.dwid_addwg);
        sname_addwg = (EditText) findViewById(R.id.sname_addwg);
        sAddress_addwg = (EditText) findViewById(R.id.sAddress_addwg);
        trTableid_addwg = (EditText) findViewById(R.id.trTableid_addwg);
        save_wg = (TextView) findViewById(R.id.save_wg);
        cancle_wg = (TextView) findViewById(R.id.cancle_wg);
        save_wg.setOnClickListener(this);
        cancle_wg.setOnClickListener(this);
        //将可选内容与ArrayAdapter连接起来
        stypeAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, stypes);
        //设置下拉列表的风格
        stypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        stype_addwg_sp.setAdapter(stypeAdapter);
        //添加事件Spinner事件监听
        stype_addwg_sp.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        stype_addwg_sp.setVisibility(View.VISIBLE);

        //将可选内容与ArrayAdapter连接起来
        stepAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, steps);
        //设置下拉列表的风格
        stepAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        step_addwg_sp.setAdapter(stepAdapter);
        //添加事件Spinner事件监听
        step_addwg_sp.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        step_addwg_sp.setVisibility(View.VISIBLE);

    }

    /*
        //使用数组形式操作
        class StypeSpinnerSelectedListener implements AdapterView.OnItemSelectedListener {

            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                       long arg3) {
                stype_addwg.setText(stypes.get(arg2).toString());
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        }

        //使用数组形式操作
        class StepSpinnerSelectedListener implements AdapterView.OnItemSelectedListener {

            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                       long arg3) {
                step_addwg.setText(steps.get(arg2).toString());
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        }
    */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_wg:
                if (//"".equals(((RegisterRolePirVO) stype_addwg_sp.getSelectedItem()).getId())
                    //|| "".equals(((RegisterRolePirVO) step_addwg_sp.getSelectedItem()).getId())
                    //|| "".equals(dwid_addwg.getText().toString().trim())
                    // ||
                        "".equals(sname_addwg.getText().toString().trim())
                    //|| "".equals(sAddress_addwg.getText().toString().trim())
                    //|| "".equals(trTableid_addwg.getText().toString().trim())
                        ) {
                    Toast.makeText(getApplicationContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
                } else {
                    final Map<String, String> map = new HashMap<String, String>();
                    WangGeBean bean = new WangGeBean();
                    map.put("stype", ((RegisterRolePirVO) stype_addwg_sp.getSelectedItem()).getId());
                    map.put("step", ((RegisterRolePirVO) step_addwg_sp.getSelectedItem()).getId());
                    map.put("dwid", dwid_addwg.getText().toString().trim());
                    map.put("saddress", sAddress_addwg.getText().toString().trim());
                    map.put("sname", sname_addwg.getText().toString().trim());
                    map.put("trtableid", trTableid_addwg.getText().toString().trim());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                OkHttpClient client = new OkHttpClient();
                                FormBody.Builder builder = new FormBody.Builder()
                                        .add("userId", String.valueOf(application.getLoginUser().getLogin_id()));
                                for (Map.Entry<String, String> m : map.entrySet()) {
                                    builder.add(m.getKey(), m.getValue());
                                }
                                RequestBody resultRequestBody = builder.build();
                                Request enttypeRequest = new Request.Builder().url(ConfUtils.SERVICE_URL + "form/doAdd/c_aj_wgsj").post(resultRequestBody).build();
                                Response enttyperesponse = client.newCall(enttypeRequest).execute();
                                if (enttyperesponse.isSuccessful()) {
                                    String result = enttyperesponse.body().string();
                                    Map<String, Object> resultMap = gson.fromJson(result, Map.class);
                                    System.out.println(resultMap);
                                    if ((boolean) resultMap.get("success")) {

                                        Message message = new Message();
                                        message.what = 1;
                                        handler.sendMessage(message);


                                    } else {
                                        Message message = new Message();
                                        message.what = 0;
                                        handler.sendMessage(message);
                                    }
                                }
                            } catch (Exception e) {
                                Message message = new Message();
                                message.what = 0;
                                handler.sendMessage(message);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }

                break;
            case R.id.cancle_wg:
                finish();
                break;
        }
    }
}

